﻿using System;
using System.Management.Automation;
using DotNet.DevOps.Tasks.Network;
using DotNet.DevOps.Tasks.Util;

namespace DotNet.DevOps.Tasks.Cmdlets
{
    /// <summary>
    /// Retreives the hostname (NET-BIOS name) of a machine from an IP address or a DNS name. Stores the result in the output variable Name.
    /// </summary>
    /// <example>
    /// import-module .\DotNet.DevOps.Tasks.dll
    /// 
    /// Get-Hostname 192.168.1.100
    /// </example> 
    [Cmdlet(VerbsCommon.Get, "Hostname", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = false, SupportsTransactions = false)]
    public class Get_Hostname : Cmdlet, IHostname
    {
        /// <summary>
        /// Name or IP address of machine to resolved the hostname (NET-BIOS name) from.
        /// </summary>
        [Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Machine { get; set; }
        /// <summary>
        /// Resulting hostname (NET-BIOS name).
        /// </summary>
        [Parameter(Mandatory = false, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Name { get; set; }

        private Hostname task;

        protected override void BeginProcessing()
        {
            task = new Hostname(new PowershellCmdletLogger(this))
                .CopyPropertyValuesFrom(this) as Hostname;
        }

        protected override void ProcessRecord()
        {
            task.Execute();
        }

        protected override void EndProcessing()
        {
            Name = task.Name;
            WriteObject(task.Name);
        }
    }
}
